<?php

namespace app\common\business;

use app\common\model\mysql\SGoodsGroup as SGoodsGroupModel;
use app\common\model\mysql\SGoodsGroupBuying as SGoodsGroupBuyingModel;

class SGroup
{
    public $goodsGroupObj = null;
    public $goodsGroupBuyingObj = null;
    public function __construct()
    {
        $this->goodsGroupObj = new SGoodsGroupModel();
        $this->goodsGroupBuyingObj = new SGoodsGroupBuyingModel();
    }

    /**
     * 查询商品列表
     * @param $where
     * @param $field
     * @param $limit
     * @param $page
     * @return array
     * @throws \think\db\exception\DbException
     */
    public function getNormalGoodsGroupData($where,$field,$limit,$page){
        $dictData = $this->goodsGroupObj->getNormalGoodsGroupList($where,$field,$limit,$page);
        if(!$dictData){
            return [];
        }
        $dictData = $dictData->toArray();
        return $dictData;
    }

    //添加拼团商品
    public function addGoodsGroup($data){
        try {
            $postNameResult = $this->getGoodsGroupByGoodsId($data['goods_id']);
            if($postNameResult){
                throw new \think\Exception("该拼团商品(主键ID:".$postNameResult['goods_group_id'].")已经存在请重新设置");
            }
            $this->goodsGroupObj->save($data);
        }catch (\Exception $e){
            throw new \think\Exception($e->getMessage());
        }
        return $this->goodsGroupObj->id;
    }

    public function getGoodsGroupByGoodsId($goods_id,$goods_group_id = ''){
        $user = $this->goodsGroupObj->getGoodsGroupByGoodsIdModel($goods_id,$goods_group_id);
        if(!$user){
            return [];
        }
        return $user->toArray();
    }

    //修改拼团商品商品状态
    public function saveStatusGoodsGroup($goods_id,$goods_status){
        $where = [
            ["goods_id", "=", $goods_id],
            ["del_flag", "=", '0'],
        ];
        $data = [
            'goods_status'=>$goods_status,
        ];
        return $this->goodsGroupObj->where($where)->find()->save($data);
    }

    //根据$id,去数据库查询是否存在这条记录
    public function getGoodsGroupById($id,$field = "*"){
        $user = $this->goodsGroupObj->getGoodsGroupByIdFind($id,$field);
        if(!$user){
            return [];
        }
        return $user->toArray();
    }

    /**
     * 根据主键id更新数据表中的数据
     * @param $id
     * @param $data
     * @return bool
     */
    public function updateById($id,$data){
        $id = intval($id);
        if(empty($id) || empty($data) || !is_array($data)){
            return false;
        }
        $postNameResult = $this->getGoodsGroupByGoodsId($data['goods_id'],$id);
        if($postNameResult){
            throw new \think\Exception("该拼团商品(主键ID:".$postNameResult['goods_group_id'].")已经存在请重新设置");
        }
        $where = [
            "goods_group_id"=>$id,
        ];
        return $this->goodsGroupObj->where($where)->find()->save($data);
    }

    //根据$ids数组，删除数据库记录delete
    public function delGoodsGroupIds($ids){
        $ids = explode(',', $ids);
        $result = $this->goodsGroupObj->DelGoodsGroupIds($ids);
        return $result;
    }

    //查询拼团商品总条数
    public function getNormalGoodsGroupCount($where){
        $postCount = $this->goodsGroupObj->getNormalGoodsGroupCount($where);
        if(!$postCount){
            return 0;
        }
        return $postCount;
    }

    /**
     * 根据主键id更新商品状态
     * @param $id
     * @param $data
     * @return bool
     */
    public function changeStatusGoods($goodsGroupId,$goodsStatus){
        $goodsGroupId = intval($goodsGroupId);
        if(empty($goodsGroupId)){
            return false;
        }
        return $this->goodsGroupObj->changeStatusGoodsModel($goodsGroupId,$goodsStatus);
    }

    /********************** 拼团列表管理 **************************/
    /**
     * 查询拼团列表
     * @param $where
     * @param $field
     * @param $limit
     * @param $page
     * @return array
     * @throws \think\db\exception\DbException
     */
    public function getNormalGoodsGroupBuyingData($where,$field,$limit,$page){
        $dictData = $this->goodsGroupBuyingObj->getNormalGoodsGroupBuyingList($where,$field,$limit,$page);
        if(!$dictData){
            return [];
        }
        $dictData = $dictData->toArray();
        return $dictData;
    }

}
